<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <!-- <script>
      console.log('1')
      setTimeout(() => {
        console.log('2')
      }, 0)
      console.log('3')
    </script> -->

    <button>测试xhr是异步函数</button>
    <script>
        document.querySelector('button').addEventListener('click', function () {
            console.log('1')
            //........................原生ajax是异步函数...........................
            const xhr = new XMLHttpRequest()
            xhr.open('post', 'http://ajax-api.itheima.net/api/login')
            xhr.setRequestHeader('content-type', 'application/json')

            xhr.addEventListener('load', function () {
                //   console.log(xhr.response)
                //   console.log(JSON.parse(xhr.response))
                console.log('2')
            })
            let obj = {
                username: 'admin',
                password: '123456',
            }
            xhr.send(JSON.stringify(obj))

            console.log('3')
        })
    </script>
</body>

</html>